using Rosetta.Editor.Creator;
using System;
using System.Collections.Generic;

namespace Rosetta.Editor.Collector
{
    [Serializable]
    public class TextCollectorBase : CollectorBase
    {
        protected Dictionary<string, string> DataList;

        protected const string RegexStr = "(\"[^\x00 -\xff]+.*?\")";

        public override void Collect(string space)
        {
            Init(space);
            foreach (var item in DataList)
            {
                if (I18NStrings != null) I18NStringPass(item.Key, item.Value);
            }
        }

        public override void Init(string space)
        {
            Clear(space);
            I18NStrings = new Dictionary<string, I18NMedia<string>>();
        }

        public override void Clear(string space)
        {
            if (I18NStrings != null) I18NStrings.Clear();
        }

        private void I18NStringPass(string text, string path = "")
        {
            if (I18NStrings.ContainsKey(text ?? throw new InvalidOperationException()))
            {
                I18NStrings[text].PathList.Add(path);
            }
            else
            {
                var i18NString = new I18NMedia<string>
                {
                    Comment = "",
                    PathList = new List<string> { path },
                    Value = text
                };
                I18NStrings.Add(text, i18NString);
            }
        }
    }
}